<!--
 * @Description: 
 * @Author: 亢超
 * @Date: 2024-11-18 18:06:09
 * @LastEditTime: 2024-11-25 23:18:22
-->
<template>
  <div>
    <el-dialog :title="cpHandler == 'add' ? '增加成品' : '编辑成品'" :visible.sync="dialogVisible" width="400px">

      <el-form ref="formCp" :model="formCp" :rules="formCpRules" label-width="80px">
        <el-form-item label="宽度" prop="kd">
          <el-input v-model="formCp.kd" size="small" placeholder="请输入宽度">
            <template>
              <template slot="append">MM</template>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="长度" prop="cd">
          <el-input v-model="formCp.cd" size="small" placeholder="条料留空">
            <template>
              <template slot="append">MM</template>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="限重" prop="xz">
          <el-input v-model="formCp.xz" size="small" placeholder="请输入限重">
            <template>
              <template slot="append">KG</template>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="公差" prop="gc">
          <el-select v-model="formCp.gc" placeholder="请选择公差" size="small" style="width: 100%;">
            <el-option label="±0.1" value="±0.1"></el-option>
            <el-option label="0~-0.1" value="0~-0.1"></el-option>
            <el-option label="0~+0.1" value="0~+0.1"></el-option>
            <el-option label="±0.05" value="±0.05"></el-option>
            <el-option label="0~-0.05" value="0~-0.05"></el-option>
            <el-option label="0~+0.05" value="0~+0.05"></el-option>
            <el-option label="±0.5" value="±0.5"></el-option>
            <el-option label="0~-0.5" value="0~-0.5"></el-option>
            <el-option label="0~+0.5" value="0~+0.5"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui';
import { validateNumberAndFolat } from '@/utils/validate.js';
export default {
  props: {

    currentData: {
      type: Array,
      default: () => []
    },
    cpHandler: {
      type: String,
      default: 'add'
    },
    currentCpData: {
      type: Array,
      default: () => []
    },
    currentCpUpdate: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      dialogVisible: false,

      formCp: {
        gc: '±0.1'
      },
      formCpRules: {
        kd: [
          { required: true, message: '宽度不能为空', trigger: 'blur' },
          { validator: validateNumberAndFolat, trigger: 'blur' }
        ],
        xz: [
          { required: true, message: '宽度不能为空', trigger: 'blur' },
          { validator: validateNumberAndFolat, trigger: 'blur' }
        ]
      },
      material: [
        { id: 1, title: '钢卷', value: '7.85' },
        { id: 2, title: '铝卷', value: '2.7' },
        { id: 3, title: '铜卷', value: '8.96' },
        { id: 4, title: '其他', value: '' },
      ]
    };
  },
  methods: {
    open () {
      this.dialogVisible = true
    },
    close () {
      // const formRef = this.currentType == 'yj' ? this.$refs.form : this.$refs.formCp
      // formRef.resetFields()
      this.dialogVisible = false
    },
    submitData () {
      const formRef = this.$refs.formCp
      formRef.validate((valid) => {
        if (valid) {
          let currentValue = JSON.parse(JSON.stringify(this.formCp))
          //判断输入宽度是否超过原卷宽度
          if (!this.isValueGreaterThanAllProperties(this.currentData, 'kd', this.formCp.kd)) return Message.warning('当前添加宽度超过最大宽度')

          if (this.currentCpData.length > 0) {
            let currentArr = []
            let otherArr = []
            if (this.formCp.id != undefined) {
              currentArr = this.currentCpData.filter(item => item.kd != this.formCp.kd && item.id != this.formCp.id)
            } else {
              currentArr = this.currentCpData.filter(item => item.kd == this.formCp.kd)
            }
            otherArr = currentArr.filter(item => item.kd == this.formCp.kd)
            if (otherArr.length > 0) return Message.warning('此长度已添加，请勿重复添加')
          }
          this.$emit('cpAddSuccess', currentValue, this.formCp.id)
          this.dialogVisible = false
          formRef.resetFields()
        }
      })
    },

    isValueGreaterThanAllProperties (arr, prop, value) {

      return arr.some(item => Number(item[prop]) > value)
    }
  },
  watch: {
    'form.cz': {
      handler (val) {
        if (val) {
          let current = this.material.find(item => item.title == val)
          this.form.md = current.value
        }
      }
    },

    currentCpUpdate: {
      handler (val) {
        if (Object.keys(val).length > 0) {
          this.formCp = val
        }
      },
      deep: true
    },
    cpHandler: {
      handler (val) {
        if (val == 'add') this.formCp.id = undefined
      }
    }
  }
}
</script>

<style scoped></style>
